package com.hncboy.chatgpt.front.service.impl;

import com.hncboy.chatgpt.base.cache.CodeCache;
import com.hncboy.chatgpt.base.config.ChatConfig;
import com.hncboy.chatgpt.base.domain.entity.ChatCustomerDO;
import com.hncboy.chatgpt.base.domain.mongodb.VerificationCode;
import com.hncboy.chatgpt.front.domain.request.VerifySecretRequest;
import com.hncboy.chatgpt.front.domain.vo.ApiModelVO;
import com.hncboy.chatgpt.front.domain.vo.ChatCustomerVO;
import com.hncboy.chatgpt.front.mongodb.VerificationCodeMapper;
import com.hncboy.chatgpt.front.service.AuthService;
import com.hncboy.chatgpt.front.service.CustomerService;
import com.hncboy.chatgpt.front.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author hncboy
 * @date 2023/3/22 20:05
 * 鉴权相关业务实现类
 */
@Slf4j
@Service
public class AuthServiceImpl implements AuthService {

    private static String phonePattern = "/^1[3|4|5|7|8][0-9]{9}$/";
    private static String emailPattern = "/^[A-Za-z\\d]+([-_.][A-Za-z\\d]+)*@([A-Za-z\\d]+[-.])+[A-Za-z\\d]{2,4}$/";
    @Value("${chat.limits}")
    Integer limits;
    @Resource
    VerificationCodeMapper verificationCodeMapper;
    @Resource
    private ChatConfig chatConfig;
    @Resource
    private CustomerService customerServiceImpl;
    @Resource
    private EmailService emailServiceImpl;

    @Override
    public ChatCustomerVO verifySecretKey(VerifySecretRequest verifySecretRequest) {
        String loginName = verifySecretRequest.getLoginName();
        String authorizationCode = verifySecretRequest.getAuthorizationCode();
        log.info("验证码为：  ======================================");
        log.info("验证码为：  " + authorizationCode);
        log.info("验证码为：  ======================================");

        VerificationCode verificationCode = verificationCodeMapper.selectByEmailAndCode(loginName, authorizationCode);
        if (verificationCode != null) {
            long difference = new Date().getTime() - verificationCode.getEnd().getTime();
            if (difference > 0) {
                throw new RuntimeException("验证码失效");
            } else {
                ChatCustomerDO customer = customerServiceImpl.getCustomer(loginName);
                return customerServiceImpl.initCustomer(customer.getId());
            }
        } else {
            throw new RuntimeException("验证码错误");
        }
    }

    @Override
    public ApiModelVO getApiModel() {
        ApiModelVO apiModelVO = new ApiModelVO();
        apiModelVO.setAuth(chatConfig.hasAuth());
        apiModelVO.setModel(chatConfig.getApiTypeEnum());
        apiModelVO.setAuthSecretKey(chatConfig.getAuthSecretKey());
        return apiModelVO;
    }


    @Override
    public ChatCustomerVO infoCustomer(Long customerId) {
        return customerServiceImpl.initCustomer(customerId);
    }

    @Override
    public boolean updateCustomer(ChatCustomerDO chatCustomerDO) {
        return customerServiceImpl.saveOrUpdate(chatCustomerDO);
    }

    @Override
    public VerificationCode send(VerifySecretRequest verifySecretRequest) {
        return emailServiceImpl.send(verifySecretRequest.getLoginName());
    }

}
